//
//  EssenceTopic.m
//  Budejie
//
//  Created by 远成物流 on 2017/5/16.
//  Copyright © 2017年 陈梦佳. All rights reserved.
//

#import "EssenceTopic.h"

@implementation EssenceTopic

-(CGFloat)cellHeight{
    // 如果已经计算过，就直接返回
    if (_cellHeight) {
        return _cellHeight;
    }
    
    // 文字的Y值
    _cellHeight += 55;
    
    //文字的高度
    NSMutableParagraphStyle* defaultStyle= [[NSMutableParagraphStyle alloc]init];
    defaultStyle.lineBreakMode = NSLineBreakByWordWrapping;
    defaultStyle.lineSpacing += 1.5;
    
    CGSize textSize = CGSizeMake(ScreenW-2*B_Margin, MAXFLOAT);
    _cellHeight += ceil([self.text boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSParagraphStyleAttributeName:defaultStyle} context:nil].size.height)+1 + B_Margin;
    
    // 中间的内容
    if (self.type != EssenceTopicTypeWord) {
        //显示图片、视频或声音
        CGFloat middleW = textSize.width;
        CGFloat middleH= middleW*self.height/self.width;
        if (middleH > ScreenH) {
            //图片长度超过屏幕为超大图片
            middleH = 200;
            self.bigPicture = YES;
        }
        
        CGFloat middleX = B_Margin;
        CGFloat middleY = _cellHeight;
        self.middleFrame = CGRectMake(middleX, middleY, middleW, middleH);
        _cellHeight += middleH + B_Margin;
    }
    
    // 最热评论
    if (self.top_cmt.count) {
        //有最热评论
        //标题
        _cellHeight += 21;
        
        //内容
        NSDictionary *cmt = self.top_cmt.firstObject;
        NSString *content = cmt[@"content"];
        if (content.length == 0) {
            content = @"[语音评论]";
        }
        
        NSString *username = cmt[@"user"][@"username"];
        NSString *cmtText = [NSString stringWithFormat:@"%@:%@",username,content];
        _cellHeight += ceil([cmtText boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.height)+1 + B_Margin;
    }
    
    //工具条
    _cellHeight += 35 + B_Margin;
    
    return _cellHeight;
}

@end
